<?php
/**
 * @see Zend_View_Helper_FormElement
 */
require_once 'Zend/View/Helper/FormElement.php';

/**
 * Помощник вида, реализующий выбор разделов сайта
 * 
 * @category Phorm
 * @package View_Helper
 * @name /Phorm/View/Helper/FormCategories.php
 * @copyright phorm.ru
 * @since 06.03.2012
 * @author vladimir@kachalov.net
 */

class Phorm_View_Helper_FormCategories extends Zend_View_Helper_FormElement {
	
	protected $_translate;
	
	public function formCategories($name, $value = array(), $attribs = array(), $options = array()) {
		
		$this->_translate = $this->view->translate()->getTranslator();
		
		$info = $this->_getInfo($name, $value, $attribs, $options);
        extract($info);
        
        /**
         * Выделяем атрибуты элемента
         */
        
        $params = array();
        foreach ($attribs as $key=>$attrib) {
        	if(is_string($attrib) && $key!='type') $params[$key] = $attrib;
        }
        
        $xhtml = array();
        
        switch (strtolower($attribs['type'])) {
        	
        	/**
        	 * Селекты
        	 */
        	
        	case 'select':
        	case 'multiselect':
        		
        		$xhtml[] = '<select name="' . $this->view->escape($name) . '"'
        				 . ' id="' . $this->view->escape($id) . '"'
        				 . $this->_htmlAttribs($params) . ">";
        				 
        		foreach ($attribs['categories'] as $key=>$category) {
					
					$xhtml[] = '<option'
							 . ' value="' . $this->view->escape($category['categoryid']) . '"'
							 . ' label="' . str_repeat('&nbsp;',$category['clevel']*4)
							 . $this->view->escape($category['categoryname']) . '"';
			                
			        if($category['visible']=='no') $xhtml[] = ' style="opacity:0.5"';
			                
					if(in_array($category['categoryid'],(array)$value)) $xhtml[] = ' selected="selected"';
			               
					$xhtml[] = '>' . $this->view->escape($category['categoryname']) . "</option>";
				}
				
				$xhtml[] = '</select>';
        		
        		break;
        		
        	/**
        	 * Чекбоксы и радиоточки
        	 */
        		
        	case 'radio':
        	case 'checkbox':
        		
        		$xhtml[] = '<ul id="' . $this->view->escape($id) . '"'.$this->_htmlAttribs($params).'>';
        		
        		foreach ($attribs['categories'] as $category) {
        			
        			// is the element disabled?
        			$disabled = '';
        			if(isset($attribs['moduleid']) && $category['moduleid']!=$attribs['moduleid']) {
        				$disabled = ' disabled="disabled"';
        			}
        			
        			// категория невидима
        			$style  = $category['visible']=='no' ? 'class="ui-state-disabled" ' : '';
        			$style .= 'style="margin-left:' . $category['clevel']*16 . 'px" ';
        			
        			// значения
        			$selected = '';
        			if($category['categoryid']==$value || (is_array($value) && in_array($category['categoryid'],$value))) {
        				$selected = ' checked="checked"';
        			}
        			
        			// тип элемента
        			$elmtype = $attribs['type']=='checkbox' ? 'checkbox' : 'radio';
        			
        			$xhtml[] = '<li ' . $style . '>';
        			$xhtml[] = '<label>' . $category['categoryname'] . '</label>';
        			$xhtml[] = '<input type="'.$elmtype.'" name="'.$name.'" value="'.$category['categoryid'].'"'.$disabled.$selected.' />';
        			$xhtml[] = '</li>';
        			
        		}
        		
        		$xhtml[] = '</ul>';
        		
        		break;
        	
        }
        
        return join(PHP_EOL,$xhtml);
		
	}
	
}